import {ScrollText} from "../ScrollText/ScrollText.js";

function messageBlock(cup) {
    this.coef = 1;
    // 获得容器
    this.cup = cup;
    if(!this.cup) return false;

    // 子组件
    this.labelTime = null;
    this.labelTitle = null;
    this.labelFrom = null;

    // 在容器内创造结构
    this.__initStruct();

    // 框架
    this.MessageBlock = this.cup.querySelector(".MessageBlock");
    // 链接
    this.aHref = this.cup.querySelector("a");
    // 图像
    this.img = this.cup.querySelector("img");
}

messageBlock.prototype.__initStruct = function() {
    let templet = `
        <div class="MessageBlock">
            <a target="_blank" href="">
                <div class="back">
                <img src="" alt="img for text">
                    <span class="rectOut">
                        <span class="rectIn">
                            <span class="labelTime"></span>
                            <span class="labelTitle"></span>
                            <span class="labelFrom"></span>
                        </span>
                    </span>
                </div>
            </a>
        </div>
    `;

    this.cup.innerHTML = templet;
    // 时间
    this.labelTime = ScrollText(this.cup.querySelector(".labelTime"));
    // 标题
    this.labelTitle = ScrollText(this.cup.querySelector(".labelTitle"));
    // 来源
    this.labelFrom = ScrollText(this.cup.querySelector(".labelFrom"));
}

// 设置网址
messageBlock.prototype.setHref = function(href) {
    if(typeof href !== "string")
        return false;
    this.aHref.href = href;
}

// 设置背景
messageBlock.prototype.setImg = function(src) {
    if(typeof src !== "string")
        return false;
    this.img.src = src;
}

// 设置时间
messageBlock.prototype.setLabelTime = function(time) {
    if(typeof time !== "string" || !this.labelTime)
        return false;
    this.labelTime.setText(time);
}

// 设置标题
messageBlock.prototype.setLabelTitle = function(title) {
    if(typeof title !== "string" || !this.labelTitle)
        return false;
    this.labelTitle.setText(title);
}

// 设置来源
messageBlock.prototype.setLabelFrom = function(from) {
    if(typeof from !== "string" || !this.labelFrom)
        return false;
    this.labelFrom.setText(from);
}

// 设置大小
messageBlock.prototype.setSize = function(coef) {
    if(!this.labelTime || !this.labelTitle || !this.labelFrom)
        return false;

    this.coef = coef || this.coef;
    this.labelTime.setRate(this.coef);
    this.labelTitle.setRate(this.coef);
    this.labelFrom.setRate(this.coef);
    this.MessageBlock.style.setProperty("--controlLen", String(window.innerWidth * this.coef))
}

function createMessageBlock(cup) {
    return new messageBlock(cup || null);
}

export var MessageBlock = createMessageBlock;